package com.frxuexi.park.view

import android.widget.SearchView.OnQueryTextListener
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.ItemTouchHelper.LEFT
import androidx.recyclerview.widget.ItemTouchHelper.SimpleCallback
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.entity.ParkBeanItem
import com.frxuexi.lib_base.mvi.BaseActivity
import com.frxuexi.lib_base.mvi.RequestType
import com.frxuexi.lib_base.mvi.UiState
import com.frxuexi.lib_base.utils.ArouterManager
import com.frxuexi.park.ParkAdapter
import com.frxuexi.park.ParkIntent
import com.frxuexi.park.ParkViewModel
import com.frxuexi.park.R
import com.frxuexi.park.databinding.ActivityParkBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@Route(path = "/park/ParkActivity")
@AndroidEntryPoint
class ParkActivity : BaseActivity<ActivityParkBinding, ParkIntent, ParkViewModel>() {
    override val binding: ActivityParkBinding by lazy { ActivityParkBinding.inflate(layoutInflater) }
    override val vm: ParkViewModel by viewModels<ParkViewModel>()
    val parkAdapter by lazy { ParkAdapter() }
    var position=0
    var content=""
    var flag=false

    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        when(it.type){
                            RequestType.DELETE->{
                                ToastUtils.showLong("删除成功")
                                parkAdapter.notifyItemRemoved(position)
                            }
                            RequestType.DEFAULT->{
                                val items = it.data as Collection<ParkBeanItem>
                                if(items.size==0){
                                    val list = parkAdapter.data.filter {
                                        it.licensePlate == content
                                    }
                                    parkAdapter.apply {
                                        data.clear()
                                        addData(list)
                                        notifyDataSetChanged()
                                    }
                                }else{
                                    parkAdapter.apply {
                                        data.clear()
                                        addData(items.sortedByDescending { it.id })
                                        notifyDataSetChanged()
                                    }
                                }
                            }
                            RequestType.AREA->{
                                val items = it.data as Collection<ParkBeanItem>
                                if(items.size==0){
                                    vm.sendIntent(ParkIntent.getParksByStatus(content))
                                }else{
                                    parkAdapter.apply {
                                        data.clear()
                                        addData(items.sortedByDescending { it.id })
                                        notifyDataSetChanged()
                                    }
                                }
                            }

                            RequestType.PARKSTATUS->{
                                val items = it.data as Collection<ParkBeanItem>
                                if(items.size==0){
                                    vm.sendIntent(ParkIntent.getParksByType(content))
                                }else{
                                    parkAdapter.apply {
                                        data.clear()
                                        addData(items.sortedByDescending { it.id })
                                        notifyDataSetChanged()
                                    }
                                }
                            }

                            RequestType.UPDATE->{
                                parkAdapter.notifyItemChanged(position)
                            }

                            else -> {}
                        }

                    }
                    is UiState.onError->{
                        ToastUtils.showLong("操作失败")
                    }

                    else -> {}
                }
            }
        }
    }

    override fun initView() {
        binding.headBack.setOnClickListener {
            finish()
        }
        binding.headTitle.setText("车位管理")
        binding.headAdd.setOnClickListener {
            ArouterManager.nav(this,"/park/ParkAddActivity")
        }

        binding.parkRv.apply {
            adapter=parkAdapter
            layoutManager=LinearLayoutManager(this@ParkActivity)
        }


        ItemTouchHelper(object :SimpleCallback(LEFT, LEFT){
            override fun onMove(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder,
                target: RecyclerView.ViewHolder
            ): Boolean {
                return false
            }

            override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
                position = viewHolder.adapterPosition
                val id = parkAdapter.data.get(position).id
                vm.sendIntent(ParkIntent.deletePark(id!!))

            }
        }).attachToRecyclerView(binding.parkRv)

        binding.parkSv.setOnQueryTextListener(object :OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                content=query!!
                if(flag){
                    vm.sendIntent(ParkIntent.getParksByArea(query.toInt()))
                    flag=false
                }else {
                    vm.sendIntent(ParkIntent.getParksByStatus(content))
                }
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {

                return true
            }
        })

        binding.parkFilter.setOnClickListener {
            flag=true
        }

        parkAdapter.setOnItemClickListener { adapter, view, position ->
            val id = parkAdapter.data.get(position).id!!
            ArouterManager.nav(this,"/park/ParkAddActivity",id)
        }

        parkAdapter.addChildClickViewIds(R.id.btn_ok,R.id.btn_no)
        parkAdapter.setOnItemChildClickListener { adapter, view, position ->
            this.position=position
            var status=""
            when(view.id){
                R.id.btn_ok->{
                    status="维护中"
                }
                R.id.btn_no->{
                    status="已占用"
                }
            }
            parkAdapter.data.get(position).spaceStatus=status
            vm.sendIntent(ParkIntent.updateParkStatus(parkAdapter.data.get(position).id!!,status))
        }

        parkAdapter.setOnItemLongClickListener { adapter, view, position ->
            ArouterManager.nav(this,"/park/ParkVRActivity")
            return@setOnItemLongClickListener true
        }
    }

    override fun onResume() {
        super.onResume()
        vm.sendIntent(ParkIntent.getParksByType("标准车位"))
    }
}